package net.wanho.personalOffice.controller;

import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.ServiceException;
import net.wanho.commons.servlet.BaseServlet;
import net.wanho.personalOffice.po.InstationMail;
import net.wanho.personalOffice.service.MailService;
import net.wanho.systemSettings.po.Employee;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/MailServlet")
public class MailServlet extends BaseServlet<MailService> {
    /*
    查找
     */
    protected String list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String receiverId = request.getParameter("receiverId");
            receiverId="5";
            Map<String,String> condMap = new LinkedHashMap<>();
            condMap.put("receiverId",receiverId);
            List<InstationMail> instationMailList = super.baseService.findByCond(condMap);
            request.setAttribute("instationMailList",instationMailList);
            return "/view/personalOffice/mail/index.jsp";
        } catch (DAOException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }

    protected String view(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return null;
    }
    protected String del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return null;
    }
    protected String upt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return null;
    }
    protected String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            InstationMail instationMail = super.parseRequest(request, InstationMail.class);
            super.baseService.save(instationMail);
            return "redirect:/MailServlet?method=list";
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }
    protected String addView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            List<Employee> employeeList = super.baseService.findEmployee();
            request.setAttribute("employeeList",employeeList);
            return "/view/personalOffice/mail/write.jsp";
        } catch (ServiceException e) {
            e.printStackTrace();
            throw new ServletException(e.getMessage());
        }
    }
}
